package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.DataStatusEnum;
import com.linkhub.oms.common.enums.mp.RoleCodeEnum;
import com.linkhub.oms.dao.mapper.UserEntityMapper;
import com.linkhub.oms.dao.mpservice.IUserService;
import com.linkhub.oms.entity.UserEntity;
import com.linkhub.oms.entity.UserRoleEntity;
import com.linkhub.oms.entity.WhiteListEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.function.UnaryOperator;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserEntityMapper, UserEntity> implements IUserService {
    @Resource
    private UserEntityMapper userEntityMapper;

    @Override
    public UserEntity getUserWithWrapper(UnaryOperator<LambdaQueryChainWrapper<UserEntity>> wrapper) {
        LambdaQueryChainWrapper<UserEntity> chainWrapper = lambdaQuery();
        return wrapper.apply(chainWrapper)
                .eq(UserEntity::getDataStatus, DataStatusEnum.ENABLE)
                .one();
    }

    @Override
    public UserEntity getUserByEmail(String email) {
        return this.getUserWithWrapper(wrapper -> wrapper.eq(UserEntity::getEmail, email));
    }

    @Override
    public UserEntity getUserByUserId(String userId) {
        return this.getUserWithWrapper(wrapper -> wrapper.eq(UserEntity::getUserId, userId));
    }

    @Override
    public void updatePassWordByEmail(String email, String password) {
        lambdaUpdate()
                .eq(UserEntity::getEmail, email)
                .set(UserEntity::getPassword, password)
                .update();
    }

    @Override
    public List<UserEntity> listUserByEmails(Collection<String> emails) {
        if (CollUtil.isEmpty(emails)) {
            return Lists.newArrayListWithCapacity(0);
        }
        return super.lambdaQuery()
                .in(UserEntity::getEmail, emails)
                .eq(UserEntity::getDataStatus, DataStatusEnum.ENABLE)
                .list();
    }

    @Override
    public List<UserEntity> listMerchants() {
        MPJLambdaWrapper<UserEntity> wrapper = JoinWrappers.lambda(UserEntity.class);
        wrapper.select(UserEntity::getUserId, UserEntity::getUserName, UserEntity::getEmail)
                .innerJoin(UserRoleEntity.class, on ->
                        on.eq(UserEntity::getUserId, UserRoleEntity::getUserId))
                .eq(UserRoleEntity::getRoleCode, RoleCodeEnum.CUSTOMER);

        return userEntityMapper.selectJoinList(UserEntity.class, wrapper);
    }

    @Override
    public void updateByUserId(String userId, UserEntity updateEntity) {

        updateEntity.setId(null);
        lambdaUpdate()
                .eq(UserEntity::getUserId, userId)
                .update(updateEntity);
    }

    @Override
    public List<UserEntity> listByUserId(List<String> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        return lambdaQuery()
                .in(UserEntity::getUserId, userIds)
                .list();
    }

    @Override
    public List<UserEntity> listInWhiteListMerchants() {
        MPJLambdaWrapper<UserEntity> wrapper = JoinWrappers.lambda(UserEntity.class);
        wrapper.select(UserEntity::getUserId, UserEntity::getUserName, UserEntity::getEmail)
                .innerJoin(UserRoleEntity.class, on ->
                        on.eq(UserEntity::getUserId, UserRoleEntity::getUserId))
                .innerJoin(WhiteListEntity.class, on ->
                        on.eq(UserEntity::getEmail, WhiteListEntity::getEmail))
                .eq(UserRoleEntity::getRoleCode, RoleCodeEnum.CUSTOMER);

        return userEntityMapper.selectJoinList(UserEntity.class, wrapper);
    }
}
